/* --------------------------------------------------------------------
	Title: toolshed
		Basic little helpers for your everyday DOM-manipulation
	
	Author: Frank Boës <mailto:fboes@berlinonline.de>
	
	Company: BerlinOnline <http://www.berlinonline.de>
	
	Created: 2005-11-14
	
	Updated: 22006-01-05
-------------------------------------------------------------------- */

// Function: $
// simplified getElementById();
function $(id) {
	return (document.getElementById(id));
}

// Function: $$
// simplified getElementsByTagName();
function $$(tag) {
	return (document.getElementsByTagName(tag));
}

// Function: $href
// Return object where id matches "#"-fraction of el.href
function $href(el) {
	return $(el.href.replace(/^.*#/,''));
}

// Function: requireOnce
// Load js-file if not already present
function requireOnce(file) {
	var scriptz = document.getElementsByTagName('script');
	for (i=0;i<scriptz.length;i++) {
		if (scriptz[i].getAttribute('src') == file) {
			return true;
		}
	}
	if (document.getElementsByTagName('head')) {
		var t = document.createElement('script');
		t.setAttribute('type', 'text/javascript');
		t.setAttribute('src', file);
		return (document.getElementsByTagName('head')[0].appendChild(t));
	}
	else {
		document.writeln('<script src="'+file+'" type="text/java'+'script"></'+'script>');
		return true;
	}
}

// Function addEvent
// Original idea by John Resig; tweaked by Scott Andrew LePera, Dean Edwards and Peter-Paul Koch
function addEvent(obj, type, fn) {
	if (obj.addEventListener)
		obj.addEventListener(type, fn, false);
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

// Function: removeEvent
// Original idea by John Resig; tweaked by Scott Andrew LePera, Dean Edwards and Peter-Paul Koch 
function removeEvent(obj, type, fn) {
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

// Function: addCladdName
function addClassName (el, cl) {
	if (el && el.className.indexOf(cl) == -1) return (el.className = (el.className)
		? el.className+' '+cl
		: cl
	);
}

// Function: removeClassName
function removeClassName (el, cl) {
	if (el) return (el.className = el.className.replace(new RegExp("\s?"+cl+"\\b"), ''));
}

// Function: toggleClassName
// Remove class if prsent, add if not present
function toggleClassName (el, cl) {
	if (el) if (el.className.indexOf(cl) != -1) {
		removeClassName(el,cl);
	}
	else {
		addClassName(el,cl);
	}
}

// Function: toggleStyleDisplay
// If object is visible, hide it and vice versa
function toggleStyleDisplay(el) {
	return el.style.display = (el.style.display != 'block') ? 'block' : 'none';
}

// Function: toggleInnerHTML
function toggleInnerHTML(el, html1, html2) {
	el.innerHTML = (el.innerHTML != html1) ? html1 : html2;
}

// Function: getElementsByAttribute
function getElementsByAttribute(attr, value, exact) {
	if (!exact)
		exact = false;
	if (!attr || !value) return false;
	if (document.all)
		{var all_obj=document.all;}
	else if (document.getElementsByTagName && !document.all)
		{var all_obj=document.getElementsByTagName("*");}
	else return false;
	var ret_obj = new Array();
	for (i=0;i<all_obj.length;i++) {
		if (all_obj[i] && all_obj[i].getAttribute(attr)) {
			if (exact == true) {
				if (all_obj[i].getAttribute(attr) == value) {
					ret_obj[ret_obj.length] = all_obj[i];
				}
			} else if  (all_obj[i].getAttribute(attr).indexOf(value)!=-1) {
					ret_obj[ret_obj.length] = all_obj[i];
			}
		}
	}
	return ret_obj;
}

// Function: getNextSibling
// get nextSibling ignoring white space nodes
function getNextSibling(el) {
	do {
		el = el.nextSibling;
	}	while (el && el.nodeType != 1);
	return (el);
}

// Function: getPreviousSibling
// get previousSibling ignoring white space nodes
function getPreviousSibling(el) {
	do {
		el = el.previousSibling;
	}	while (el && el.nodeType != 1);
	return (el);
}

// Function: getFirstChild
// get firstChild ignoring white space nodes
function getFirstChild(el) {
	el = el.firstChild;
	while (el && el.nodeType != 1) {
		el = el.nextSibling;
	}
	return (el);
}

// Function: getLastChild
// get lastChild ignoring white space nodes
function getLastChild(el) {
	el = el.lastChild;
	while (el && el.nodeType != 1) {
		el = el.previousSibling;
	}
	return (el);
}

// Function: getParentNode
// get parentNode ignoring white space nodes
function getParentNode(el) {
	el = el.parentNode;
	while (el && el.nodeType != 1) {
		el = el.parentNode;
	}
	return (el);
}

// Function: getChildNodes
// get childNodes ignoring white space nodes
function getChildNodes(el) {
  var objs = new Array();
  var i = 0;
  el = el.firstChild;
  while (el) {
    if (el.nodeType == 1) {objs[i++] = el;}
    el = el.nextSibling;
  }
  return objs;
}

// Function: getAncestorsByTagname
// search for ancestors containing a specific tagname
function getAncestorsByTagname(el,nodename) {
    while (el && el.nodeName != nodename) {
        el = el.parentNode;
    }
  return el;
}

// Function: focusElement
// get position if an element and focus it on the screen
function focusElement(el) {
  if (! el.offsetLeft)
    el=document.getElementById(el)
    if (! el.offsetLeft)
      return false;
  window.scrollTo(el.offsetLeft, el.offsetTop);
} 

// Function: insertAfter
// Insert DOM-Node as nextSibling
function insertAfter(src,tgt) {
	if (tgt.nextSibling)
		tgt.parentNode.insertBefore(src, tgt.nextSibling);
	else 
		tgt.parentNode.appendChild(src);
}

// for including toolshed.js in bookmarklets
var toolshed_loaded = true;